<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../static/lib/layui-v2.9.8/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="form-filter">
    <div class="layui-form-item">
        <h2 align="center">新增商品</h2>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">商品名字</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="商品名称不能为空"
                   placeholder="请输入商品名字" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">规格</label>
        <div class="layui-input-block">
            <input type="text" name="specification" lay-reqtext="规格不能为空" placeholder="请输入规格" value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">价格</label>
        <div class="layui-input-block">
            <input type="number" name="price" lay-reqtext="价格不能为空" placeholder="请输入价格" value=""
                   class="layui-input" lay-precision="2" step='0.01' lay-affix="number" min="0">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">数量</label>
        <div class="layui-input-block">
            <input type="number" name="quantity" lay-reqtext="数量不能为空" placeholder="请输入数量" value=""
                   class="layui-input" lay-precision="0" lay-affix="number" min="0">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../../static/lib/layui-v2.9.8/layui.js" charset="utf-8"></script>


<script>

    function parent_to_child(row) {
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;
            // ajax 头部增加csrf_token
            var token = "{{ csrf_token }}"
            $.ajaxSetup({
                headers: {
                    'X-CSRFTOKEN': `${token}`,
                    'AUTHORIZATION': 'Bearer ' + localStorage.token,
                }, // JWTAuthentication.authenticate()方法获取 header = request.META.get(api_settings.AUTH_HEADER_NAME),
            });

            $.ajaxSetup({
                complete: function (XMLHttpRequest, textStatus) {
                    if (XMLHttpRequest.status === 401) {  // 401 表示未授权，即 token 无效
                        layer.msg('授权已过期，需重新登陆', {icon: 2, time: 2000}, function () {
                            window.location.href = '/login';
                        })
                    }
                }
            });

            $(function () {   //页面加载时，AJAX请求
                $.ajax({
                    url: '/auth-query',
                    type: 'post',
                    dataType: 'json',
                    success: function (result) {
                        console.log(result);
                    },
                    error: function () {
                        layer.msg('授权查询API出错！');
                    }
                })
            });
            //回显数据
            form.val('form-filter', row.data);
            //监听提交
            form.on('submit(saveBtn)', function (data) {
                let fields = data.field
                fields['id'] = row.data.id;
                //传给后台
                $.ajax({
                    url: "/product_manage_edit",
                    type: "post",
                    data: data.field,
                    success: function (result) {
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 2000}, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                                parent.layui.table.reload('currentTableId');
                            })
                        } else {
                            layer.alert(result.msg);
                        }
                    },
                    error: function (result) {
                        alert("接口错误！！！");
                    }
                });

                return false;

            });

        });
    }
</script>
</body>
</html>